package com.sl.modulars.trans.equipment.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sl.modulars.trans.device.entity.EvoDeviceType;
import com.sl.modulars.trans.equipment.entity.*;
import com.sl.modulars.trans.equipment.entity.query.*;
import com.sl.modulars.trans.equipment.entity.vo.*;
import com.sl.modulars.trans.equipment.mapper.EquipmentMapper;
import com.sl.modulars.trans.equipment.service.IEquipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EquipmentServiceImpl implements IEquipmentService {


    @Autowired
    private EquipmentMapper equipmentMapper;


    /**
     * 分页 查询设备清单数据
     * @param odsGmdcMachineQuery
     * @return
     */
    @Override
    public PageInfo<OdsGmdcMachineVO> getOdsGmdcMachinePage(OdsGmdcMachineQuery odsGmdcMachineQuery) {
        PageHelper.startPage(odsGmdcMachineQuery.getPageNum(), odsGmdcMachineQuery.getPageSize());
        PageInfo<OdsGmdcMachineVO> pageInfo = new PageInfo<OdsGmdcMachineVO>(equipmentMapper.getOdsGmdcMachinePage(odsGmdcMachineQuery));
        return pageInfo;
    }





    /**
     * 从设备状态维度查询对应的数据
     * @param groupId 分组编号可传递或不传递
     * @return
     */
    @Override
    public Map<String,Object> getOdsGmdcMachineCount(String groupId){
        Map<String,Object> map=new HashMap<>();
        map.put("machineCount",equipmentMapper.getOdsGmdcMachineCount(groupId));
        map.put("normalCount",equipmentMapper.getOdsGmdcMachineNormal(groupId));
        map.put("abnormalCount",equipmentMapper.getOdsGmdcMachineAbnormal(groupId));
        return map;
    }




    /**
     * 根据设备资产编号 查询设备运行参数最新的一条数据
     * @param machineNumber
     * @return
     */
    @Override
    public OdsGmdcParamMachine getOdsGmdcParamMachineByMachineNumber(String machineNumber){
       return equipmentMapper.getOdsGmdcParamMachineByMachineNumber(machineNumber);
    }






    /**
     * 根据设备资产编号查询设备详情数据
     * @param machineNumber 设备资产编号
     * @return
     */
    @Override
    public OdsGmdcMachineVO getOdsGmdcMachineByMachineNumber(String machineNumber){
        return equipmentMapper.getOdsGmdcMachineByMachineNumber(machineNumber);
    }


    /**
     * 查询某个设备的设备数据详情
     * @param machineNumber
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    public List<OdsGmdcParamMachine> getOdsGmdcParamMachineByMachineNumberAndTime(String machineNumber, String startTime, String endTime){
        return equipmentMapper.getOdsGmdcParamMachineByMachineNumberAndTime(machineNumber,startTime,endTime);
    }


    /**
     * 查询工装信息列表分页数据信息
     * @param odsGmdcToolingThingsQuery
     * @return
     */
    @Override
    public PageInfo<OdsGmdcToolingThings> getOdsGmdcToolingThingPage(OdsGmdcToolingThingsQuery odsGmdcToolingThingsQuery){
        PageHelper.startPage(odsGmdcToolingThingsQuery.getPageNum(), odsGmdcToolingThingsQuery.getPageSize());
        PageInfo<OdsGmdcToolingThings> pageInfo = new PageInfo<OdsGmdcToolingThings>(equipmentMapper.getOdsGmdcToolingThingPage(odsGmdcToolingThingsQuery));
        return pageInfo;
    }


    /**
     * 查询工装信息统计数据
     * @param groupId
     * @return
     */
    @Override
    public Map<String, Object> getOdsGmdcToolingThingCount(String groupId) {
        Map<String,Object> map=new HashMap<>();
        map.put("lingThingCount",equipmentMapper.getOdsGmdcToolingThingCount(groupId));
        map.put("availableCount",equipmentMapper.getOdsGmdcToolingThingAvailable(groupId));
        map.put("reuseCount",equipmentMapper.getOdsGmdcToolingThingReuse(groupId));
        return map;
    }



    /**
     * 查询工刀量具列表数据
     * @param odsGmdcMeasuringToolsQuery
     * @return
     */
    public PageInfo<OdsGmdcMeasuringTools> getOdsGmdcMeasuringToolsPage(OdsGmdcMeasuringToolsQuery odsGmdcMeasuringToolsQuery){
        PageHelper.startPage(odsGmdcMeasuringToolsQuery.getPageNum(), odsGmdcMeasuringToolsQuery.getPageSize());
        PageInfo<OdsGmdcMeasuringTools> pageInfo = new PageInfo<OdsGmdcMeasuringTools>(equipmentMapper.getOdsGmdcMeasuringToolsPage(odsGmdcMeasuringToolsQuery));
        return pageInfo;
    }



    /**
     * 查询工刀量具设备数据
     * @param groupId
     * @return
     */
   public Map<String,Object> getOdsGmdcMeasuringToolsCount(String groupId){
       Map<String,Object> map=new HashMap<>();
       map.put("measuringTools",equipmentMapper.getOdsGmdcMeasuringToolsCount(groupId));
       map.put("availableCount",equipmentMapper.getOdsGmdcMeasuringToolsAvailable(groupId));
       map.put("reuseCount",equipmentMapper.getOdsGmdcMeasuringToolsReuse(groupId));
       return map;
   }


    /**
     * 查询物料编码列表数据
     * @param odsGmdcMaterialQuery
     * @return
     */
    @Override
    public PageInfo<OdsGmdcMaterialVO> getOdsGmdcMaterialPage(OdsGmdcMaterialQuery odsGmdcMaterialQuery) {
        PageHelper.startPage(odsGmdcMaterialQuery.getPageNum(), odsGmdcMaterialQuery.getPageSize());
        PageInfo<OdsGmdcMaterialVO> pageInfo = new PageInfo<OdsGmdcMaterialVO>(equipmentMapper.getOdsGmdcMaterialPage(odsGmdcMaterialQuery));
        return pageInfo;
    }

    /**
     * 查询物料总数
     * @param groupId
     * @return
     */
    @Override
    public Map<String,Object> getOdsGmdcMaterialCount(String groupId){
        return equipmentMapper.getOdsGmdcMaterialCount(groupId);
    }


    /**
     * 查询用户列表数据
     * @param odsGmdcPersonnelQuery
     * @return
     */
    @Override
    public PageInfo<OdsGmdcPersonnel> getOdsGmdcPersonnelPage(OdsGmdcPersonnelQuery odsGmdcPersonnelQuery){
        PageHelper.startPage(odsGmdcPersonnelQuery.getPageNum(), odsGmdcPersonnelQuery.getPageSize());
        PageInfo<OdsGmdcPersonnel> pageInfo = new PageInfo<OdsGmdcPersonnel>(equipmentMapper.getOdsGmdcPersonnelPage(odsGmdcPersonnelQuery));
        return pageInfo;
//        return equipmentMapper.getOdsGmdcPersonnelPage(odsGmdcPersonnelQuery);
    }



    /**
     * 构建基础树状结构（不含设备结构）
     * @param treeType
     * @return
     */
    public List<OdsTreeVO> getOdsTreeByType(String treeType){
        Map<String,OdsTreeVO> treeMap=new HashMap<>();
        //构建树结构（不含树状结构）
        List<OdsTree> odsTrees=equipmentMapper.getOdsTree(treeType);
        //然后定义List
        List<OdsTreeVO> list=new ArrayList<>();
        for (OdsTree odsTree:odsTrees){
            OdsTreeVO odsTreeVO=new OdsTreeVO();
            odsTreeVO.setLabel(odsTree.getName());
            odsTreeVO.setValue(odsTree.getId());
            odsTreeVO.setParentId(odsTree.getParentId());
            treeMap.put(odsTree.getId(),odsTreeVO);
        }



        for (OdsTree odsTree:odsTrees){
            OdsTreeVO node=treeMap.get(odsTree.getId());
            if (odsTree.getParentId().equals("0")){
                list.add(node);
            }
            else {
                OdsTreeVO parNode=treeMap.get(odsTree.getParentId());
                if (parNode!=null){
                    parNode.getChildren().add(node);
                }
            }
        }
        return list;
    }



    /**
     * 构建基础树状结构（含设备结构）
     * @param treeType
     * @return
     */
    public List<OdsTreeVO> getOdsTreeByTypeDevice(String treeType){
        Map<String,OdsTreeVO> treeMap=new HashMap<>();
        //构建树结构（不含树状结构）
        List<OdsTree> odsTrees=equipmentMapper.getOdsTree(treeType);


        OdsGmdcMachineQuery odsGmdcMachineQuery=new OdsGmdcMachineQuery();
        List<OdsGmdcMachineVO> device= equipmentMapper.getOdsGmdcMachinePage(odsGmdcMachineQuery);
        //然后定义List
        List<OdsTreeVO> list=new ArrayList<>();
        for (OdsTree odsTree:odsTrees){
            OdsTreeVO odsTreeVO=new OdsTreeVO();
            odsTreeVO.setLabel(odsTree.getName());
            odsTreeVO.setValue(odsTree.getId());
            odsTreeVO.setParentId(odsTree.getParentId());
            treeMap.put(odsTree.getId(),odsTreeVO);
        }



        for (OdsTree odsTree:odsTrees){
            OdsTreeVO node=treeMap.get(odsTree.getId());
            if (odsTree.getParentId().equals("0")){
                list.add(node);
            }
            else {
                OdsTreeVO parNode=treeMap.get(odsTree.getParentId());
                if (parNode!=null){
                    parNode.getChildren().add(node);
                }
            }
        }


        for (OdsGmdcMachineVO odsGmdcMachineVO:device){
            OdsTreeVO node=treeMap.get(odsGmdcMachineVO.getGroupId());
            if (node!=null){
                OdsTreeVO odsTree=new OdsTreeVO();
                odsTree.setValue(odsGmdcMachineVO.getMachineNumber());
                odsTree.setLabel(odsGmdcMachineVO.getMachineName());
                odsTree.setParentId(node.getValue());
                node.getChildren().add(odsTree);
            }
        }
        return list;
    }




    /**
     * 查询人员数据统计详情
     * @param groupId
     * @return
     */
   public Map<String,Object> getOdsGmdcPersonnelCount(String groupId){
       return equipmentMapper.getOdsGmdcPersonnelCount(groupId);
   }


    /**
     * 查询计量设备列表数据
     * @param odsGmdcMeteringEquipmentQuery
     * @return
     */
   public PageInfo<OdsGmdcMeteringEquipment> getOdsGmdcMeteringEquipmentPage(OdsGmdcMeteringEquipmentQuery odsGmdcMeteringEquipmentQuery){
       PageHelper.startPage(odsGmdcMeteringEquipmentQuery.getPageNum(), odsGmdcMeteringEquipmentQuery.getPageSize());
       PageInfo<OdsGmdcMeteringEquipment> pageInfo = new PageInfo<OdsGmdcMeteringEquipment>(equipmentMapper.getOdsGmdcMeteringEquipmentPage(odsGmdcMeteringEquipmentQuery));
       return pageInfo;
   }



    /**
     * 查询计量设备 展示维度数据
     * @param groupId
     * @return
     */
   public Map<String,Object> getOdsGmdcMeteringEquipmentCount(String groupId){
       return equipmentMapper.getOdsGmdcMeteringEquipmentCount(groupId);
   }




    /**
     * 查询测量设备树结构
     * @param treeType
     * @return
     */
   public List<OdsTreeVO> getOdsTreeByTypeGmdcMetering(String treeType){
       Map<String,OdsTreeVO> treeMap=new HashMap<>();
       //构建树结构（不含树状结构）
       List<OdsTree> odsTrees=equipmentMapper.getOdsTree(treeType);


       OdsGmdcMeteringEquipmentQuery odsGmdcMeteringEquipmentQuery=new OdsGmdcMeteringEquipmentQuery();
       List<OdsGmdcMeteringEquipment> device= equipmentMapper.getOdsGmdcMeteringEquipmentPage(odsGmdcMeteringEquipmentQuery);
       //然后定义List
       List<OdsTreeVO> list=new ArrayList<>();
       for (OdsTree odsTree:odsTrees){
           OdsTreeVO odsTreeVO=new OdsTreeVO();
           odsTreeVO.setLabel(odsTree.getName());
           odsTreeVO.setValue(odsTree.getId());
           odsTreeVO.setParentId(odsTree.getParentId());
           treeMap.put(odsTree.getId(),odsTreeVO);
       }



       for (OdsTree odsTree:odsTrees){
           OdsTreeVO node=treeMap.get(odsTree.getId());
           if (odsTree.getParentId().equals("0")){
               list.add(node);
           }
           else {
               OdsTreeVO parNode=treeMap.get(odsTree.getParentId());
               if (parNode!=null){
                   parNode.getChildren().add(node);
               }
           }
       }


       for (OdsGmdcMeteringEquipment odsGmdcMeteringEquipment:device){
           OdsTreeVO node=treeMap.get(odsGmdcMeteringEquipment.getGroupId());
           if (node!=null){
               OdsTreeVO odsTree=new OdsTreeVO();
               odsTree.setValue(odsGmdcMeteringEquipment.getDeviceId());
               odsTree.setLabel(odsGmdcMeteringEquipment.getDeviceName());
               odsTree.setParentId(node.getValue());
               node.getChildren().add(odsTree);
           }
       }
       return list;
   }




    /**
     * 查询计量设备采集详情 分页查询
     * @param odsFileGmdcEquipmentGatherQuery
     * @return
     */
    public PageInfo<OdsFileGmdcEquipmentGatherVO> getOdsFileGmdcEquipmentGather(OdsFileGmdcEquipmentGatherQuery odsFileGmdcEquipmentGatherQuery){
        PageHelper.startPage(odsFileGmdcEquipmentGatherQuery.getPageNum(), odsFileGmdcEquipmentGatherQuery.getPageSize());
        PageInfo<OdsFileGmdcEquipmentGatherVO> pageInfo = new PageInfo<OdsFileGmdcEquipmentGatherVO>(equipmentMapper.getOdsFileGmdcEquipmentGather(odsFileGmdcEquipmentGatherQuery));
        return pageInfo;
    }


    /**
     * 查询指标卡 计量设备采集
     * @return deviceId
     */
    public Map<String,Object> getOdsTreeByTypeGmdcMeteringCount(String deviceId){
        Map<String,Object> map=equipmentMapper.getOdsTreeByTypeGmdcMeteringCount(deviceId);
        map.put("num",equipmentMapper.getOdsGmdcMeteringEquipmentCountNum());
        return map;
    }




    /**
     * 查看采集数据详情
     * @param odsFileGmdcEquipmentGatherQuery
     * @return
     */
   public PageInfo<OdsFileEquipmenCaptureDetails> getOdsFileEquipmenCaptureDetailsByGid(OdsFileEquipmenCaptureDetailsQuery odsFileGmdcEquipmentGatherQuery){
       PageHelper.startPage(odsFileGmdcEquipmentGatherQuery.getPageNum(), odsFileGmdcEquipmentGatherQuery.getPageSize());
       List<OdsFileEquipmenCaptureDetails> list=equipmentMapper.getOdsFileEquipmenCaptureDetailsByGid(odsFileGmdcEquipmentGatherQuery);
       PageInfo<OdsFileEquipmenCaptureDetails> pageInfo = new PageInfo<OdsFileEquipmenCaptureDetails>(list);
       return pageInfo;
   }


    /**
     * 查询环境折线图
     * @param strSn
     * @return
     */
   public List<DwdEnvironmentData> getDwdEnvironmentDataByEnvironmentByStrSn(String strSn){
       return equipmentMapper.getDwdEnvironmentDataByEnvironmentByStrSn(strSn);
   }


    /**
     * 查询环境列表数据
     * @param dwdEnvironmentDataQuery
     * @return
     */
    public PageInfo<DwdEnvironmentDataVO> getDwdEnvironmentDataPage(DwdEnvironmentDataQuery dwdEnvironmentDataQuery){
        PageHelper.startPage(dwdEnvironmentDataQuery.getPageNum(), dwdEnvironmentDataQuery.getPageSize());
        PageInfo<DwdEnvironmentDataVO> pageInfo = new PageInfo<DwdEnvironmentDataVO>(equipmentMapper.getDwdEnvironmentDataPage(dwdEnvironmentDataQuery));
        return pageInfo;
    }




    /**
     * 查询环境 采集数量 异常环境数 查询环境设备总数
     * @param strSn
     * @return
     */
    public Map<String,Object> getDwdEnvironmentDataByStrSn(String strSn){
        Map<String,Object> map=equipmentMapper.getDwdEnvironmentDataByStrSn(strSn);
        if(map==null){
            map=new HashMap<>();
        }
        String num=equipmentMapper.getDwdEnvironmentCount();
        map.put("num",num);
        return map;
    }




    /**
     * 查询测量设备树结构
     * @param treeType
     * @return
     */
    public List<OdsTreeVO> getOdsTreeByTypeEnvironmentData(String treeType){
        Map<String,OdsTreeVO> treeMap=new HashMap<>();
        //构建树结构（不含树状结构）
        List<OdsTree> odsTrees=equipmentMapper.getOdsTree(treeType);


        List<DwdEnvironment> device= equipmentMapper.getDwdEnvironmentDataList(null);
        //然后定义List
        List<OdsTreeVO> list=new ArrayList<>();
        for (OdsTree odsTree:odsTrees){
            OdsTreeVO odsTreeVO=new OdsTreeVO();
            odsTreeVO.setLabel(odsTree.getName());
            odsTreeVO.setValue(odsTree.getId());
            odsTreeVO.setParentId(odsTree.getParentId());
            treeMap.put(odsTree.getId(),odsTreeVO);
        }



        for (OdsTree odsTree:odsTrees){
            OdsTreeVO node=treeMap.get(odsTree.getId());
            if (odsTree.getParentId().equals("0")){
                list.add(node);
            }
            else {
                OdsTreeVO parNode=treeMap.get(odsTree.getParentId());
                if (parNode!=null){
                    parNode.getChildren().add(node);
                }
            }
        }


        for (DwdEnvironment dwdEnvironment:device){
            OdsTreeVO node=treeMap.get(dwdEnvironment.getGroupId());
            if (node!=null){
                OdsTreeVO odsTree=new OdsTreeVO();
                odsTree.setValue(dwdEnvironment.getStrSn());
                odsTree.setLabel(dwdEnvironment.getStrName());
                odsTree.setParentId(node.getValue());
                node.getChildren().add(odsTree);
            }
        }
        return list;
    }

    @Override
    public OdsGmdcMachineVO getOdsGmdcMachineByNum(String machineNumber) {
        return equipmentMapper.getOdsGmdcMachineByNum(machineNumber);
    }

}
